<?php

namespace App\Task\v1_1_0;

use App\Constants\InviteVideoCode;
use App\Helpers\EventUtil;
use App\Model\CallBase;
use App\Model\Message;
use App\Model\Service\ConsumeService;
use App\Model\UserFreeCall;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\ServiceTrait\GetUserInfoTrait;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 邀请的视频(时长和费用)
 * @Crontab(name="CheckInviteCallTask", rule="*\/1 * * * * *", callback="execute", memo="检测视频邀约通话",singleton=false, enable=true)
 */
class CheckInviteCallTask
{
    use GetUserInfoTrait;

    public function execute(){
        $this->checkCalls(VideoCall::CALL_TYPE);
        $this->checkCalls(VoiceCall::CALL_TYPE);
    }

    /**
     * 检查通话
     */
    function checkCalls($callType = VoiceCall::CALL_TYPE)
    {
        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;
        //获取当前正在通话的语聊记录
        $callingLogs = $callCls::getCallsForInviteVideo(CallBase::STATUS_ING);
        $count = count($callingLogs);
        log_info(__CLASS__ . "==docheck==CallType:{$callType}==Count:{$count}");
        if ($count == 0) return;
        array_walk($callingLogs, function ($callingLog) use ($callType, $callCls) {
            //已经拨打时间
            $callSeconds = time() - $callingLog['answer_time'];

            //免费时间
            $freeSeconds = InviteVideoCode::FREE_TIME * 60;

            if ($callSeconds >= ($freeSeconds - 20)) {
                //结算单
//                $this->endCall($callingLog, $callCls);

                $userInfo = $this->getUserInfo($callingLog['chat_user_id']);

                if ($userInfo->gender == 1) {
                    //福利时间用完，中止通话- 透传消息
//                    Message::sendSlient($callingLog['user_id'], 'HANGUP_CALL', ['tip' => '视频约会通话已结束，记得查收奖励哦～']);
                    Message::sendSlient($callingLog['chat_user_id'], 'FREE_BALANCE_TIP', ['tip' => '你的免费福利时长已经用完了，此次通话继续的话会按照'.$callingLog['coin_setting'].'/分钟计费~']);

                    //发系统消息
//                    Message::pushOfficialMsg($callingLog['chat_user_id'], "你的免费通话时长已用完，接下来的通话计费~");
//                    Message::pushOfficialMsg($callingLog['user_id'], "视频约会通话已结束，记得查收奖励哦～");
                }else{
                    //福利时间用完，中止通话- 透传消息
//                    Message::sendSlient($callingLog['chat_user_id'], 'HANGUP_CALL', ['tip' => '视频约会通话已结束，记得查收奖励哦～']);
                    Message::sendSlient($callingLog['chat_user_id'], 'FREE_BALANCE_TIP', ['tip' => '你的免费福利时长已经用完了，此次通话继续的话会按照'.$callingLog['coin_setting'].'/分钟计费~']);

                    //发系统消息
//                    Message::pushOfficialMsg($callingLog['user_id'], "你的免费通话时长已用完，通话已结束~");
//                    Message::pushOfficialMsg($callingLog['chat_user_id'], "视频约会通话已结束，记得查收奖励哦～");
                }

                log_info(__CLASS__ . "==overtime==CallType:{$callType}==TotalTime:{$callSeconds}s==LogId:{$callingLog['id']}");

                return;
            }
//
//            if($callSeconds >= ($freeSeconds - 15)){
//                $userInfo = $this->getUserInfo($callingLog['chat_user_id']);
//
//                if ($userInfo->gender == 1) {
//                    Message::sendSlient($callingLog['chat_user_id'], 'FREE_BALANCE_TIP', ['title' => '温馨提示','sub_title' => '免费通话即将结束，充值后可与她继续畅聊哦~']);
//                }else{
//                    Message::sendSlient($callingLog['user_id'], 'FREE_BALANCE_TIP', ['title' => '温馨提示','sub_title' => '免费通话即将结束，充值后可与她继续畅聊哦~']);
//                }
//                return;
//            }

            log_info(__CLASS__ . "==passcheck==CallType:{$callType}==TotalTime:{$callSeconds}s==LogId:{$callingLog['id']}");
        });
    }

    /**
     * 结算话单
     */
    function endCall($callRecord = null, $callCls = VideoCall::class, $freeCallLog = null)
    {
        if (is_array($callRecord)) $callRecord = $callCls::find($callRecord['id']);
        //离开频道，更新话单状态
        $callRecord = CallBase::channelOutUpdate($callRecord, 1);
        if (!$callRecord) return;
        //通话消费处理
        (new ConsumeService)->settleCallForInvite($callRecord, $callCls::CALL_TYPE);
        //挂断事件分发
        EventUtil::dispatchHangup(
            $callRecord,
            $callCls::CALL_TYPE == VideoCall::CALL_TYPE ? 'video' : 'voice'
        );
        log_info(__CLASS__ . "=={$callCls}==EndCall==LogId:{$callRecord->id}");
    }
}
